<template>
  <div class="com-container">
    <div class="com-chart" ref="hot"></div>
    <span class="left-icon" :style="comStyle" @click="toLeft">◀</span>
    <span class="right-icon" :style="comStyle" @click="toRight">▶</span>
    <div class="cat-name" :style="comStyle">{{ catName }}</div>
  </div>
</template>

<script>
export default {
  created() {
    // 在组件创建完成后，进行回调函数注册
    this.$socket.registerCallBack("hotData", this.getData);
  },
  mounted() {
    // 初始化Charts
    this.initChart();
    // 获取数据
    // this.getData();
    // 告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "hotData",
      chartName: "hotproduct",
      value: "",
    });
    // 适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("hotData");
  },
  data() {
    return {
      // 图表的实例对象
      chartInstance: null,
      // 从服务器中获取的所有数据
      allData: null,
      // 当前所展示的一级分类数据
      currentIndex: 0,

      titleFontSize: null,
    };
  },
  computed: {
    catName() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.currentIndex].name;
      }
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.hot, "chalk");

      const initOption = {
        title: {
          text: "▎热销商品的占比",
          left: 20,
          top: 20,
        },
        legend: {
          top: "14%",
          left: "2%",
          icon: "circle",
        },
        tooltip: {
          show: true,
          backgroundColor: "#222733",
          borderColor: "transparent",
          formatter: (arg) => {
            console.log(arg.data);
            const thirdCategory = arg.data.children;
            //  计算所有三级分类数值总和
            let total = 0;
            thirdCategory.forEach((item) => {
              total += item.value;
            });
            let str = "";
            thirdCategory.forEach((item) => {
              str += `
              <div style="color:#fff;display:flex;align-items: center;">
                <div style="margin-right:5px;height: 10px;width:10px;border-radius: 50%;background: #2c6eff;display: inline-block;"></div>
                ${item.name}: ${parseInt((item.value / total) * 100)}%
                <br/>
              </div> 
              `;
            });
            return str;
          },
        },
        series: [
          {
            type: "pie",
            label: {
              show: false,
            },
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    getData(res) {
      // 获取地图的散点数据
      // const { data: res } = await this.$http.get("hotproduct");
      this.allData = res;
      console.log(res);
      this.updateChart();
    },
    updateChart() {
      const legendDate = this.allData[this.currentIndex].children.map(
        (item) => {
          return item.name;
        }
      );
      const seriesData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
            value: item.value,
            children: item.children,
          };
        }
      );
      let dataOption = {
        legend: {
          data: legendDate,
        },
        series: [
          {
            data: seriesData,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    // 点击左侧按钮
    toLeft() {
      this.currentIndex--;
      // 已到达最左边
      if (this.currentIndex < 0) this.currentIndex = this.allData.length - 1;
      this.updateChart();
    },
    // 点击右侧按钮
    toRight() {
      this.currentIndex++;
      // 已到达最右边
      if (this.currentIndex > this.allData.length - 1) this.currentIndex = 0;
      this.updateChart();
    },
    // 浏览器大小发生变化会调用此方法
    screenAdapter() {
      this.titleFontSize = (this.$refs.hot.offsetWidth / 100) * 3.6;

      let adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        legend: {
          itemWidth: this.titleFontSize / 2,
          itemHeight: this.titleFontSize / 2,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 1.2,
          },
        },
        series: [
          {
            radius: this.titleFontSize * 5,
            center: ["50%", "60%"],
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>

<style lang="less" scoped>
.com-container {
  position: relative;
  .left-icon {
    position: absolute;
    left: 6%;
    top: 60%;
    transform: translateY(-50%);
    color: #fff;
    cursor: pointer;
  }
  .right-icon {
    position: absolute;
    right: 6%;
    top: 60%;
    transform: translateY(-50%);
    color: #fff;
    cursor: pointer;
  }
  .cat-name {
    position: absolute;
    left: 80%;
    bottom: 20px;
    color: #fff;
  }
}
</style>
